package com.zhzhinn.activity;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzBaseTooolBarActivity;
import com.edgclub.edg.activity.GzWebViewActivity;
import com.edgclub.edg.activity.RegisterSetPassActivity;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.UserApi;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.Timer;
import java.util.TimerTask;

public class ZZRegisterActivity extends GzBaseTooolBarActivity implements View.OnClickListener , VolleyListener{
    private EditText phone , password;
    private Button submit;
    private TextView user_protocol;

    private RelativeLayout user_rules;

    @Override
    public int getContentView() {
        return R.layout.zz_activity_register;
    }

    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("注册");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
    }

    @Override
    public void initView() {
        phone = (EditText) findViewById(R.id.phone);
        password = (EditText) findViewById(R.id.password);
        user_rules = (RelativeLayout) findViewById(R.id.user_rules);
        user_rules.setVisibility(View.VISIBLE);
        submit = (Button) findViewById(R.id.submit);
        user_protocol = (TextView) findViewById(R.id.user_protocol);

        submit.setOnClickListener(this);
        user_protocol.setOnClickListener(this);

    }

    @Override
    public void initData() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.submit:
                String strPhone = phone.getText().toString().trim();
                if(TextUtils.isEmpty(strPhone)){
                    Toast.makeText(ZZRegisterActivity.this,"请输入你的手机号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!StringUtils.isPhoneNum(strPhone)) {
                    Toast.makeText(ZZRegisterActivity.this,"手机格式不正确",Toast.LENGTH_SHORT).show();
                    return;
                }
                String psw = password.getText().toString().trim();
                if(TextUtils.isEmpty(psw)){
                    Toast.makeText(ZZRegisterActivity.this,"请设置你的密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!StringUtils.isMatchPsw(psw)) {
                    Toast.makeText(ZZRegisterActivity.this,"请输入8-24位密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                getCode();
                break;
            case R.id.main_back:
                finish();
                break;
            case R.id.user_protocol:
                Intent intent1=new Intent(this,GzWebViewActivity.class);
                intent1.putExtra("url", ApiConfig.USER_PROTOCOL_URL);
                intent1.putExtra("right_btn",true);
                startActivity(intent1);
                break;
        }
    }

    private void getCode(){
        String strPhone = phone.getText().toString().trim();
        UserParam param = new UserParam();
        param.setUid(strPhone);
        param.setTarget("register");
        new UserApi(this).registerGetcode(param,this);

    }

    @Override
    public void onStart(String tag) {
        showDialog(StringUtils.getString(R.string.get_login), false);
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        if (ApiConfig.APIURL.URL_REGISTER_CODE .equals(tag)) {
            if (JSONHelper.isSuccess(response)) {
                // 跳转至下个而面
                Intent intent = new Intent(ZZRegisterActivity.this , ZZVerificationActivity.class);
                intent.putExtra("phone" , phone.getText().toString().trim());
                intent.putExtra("password" , password.getText().toString().trim());
                startActivity(intent);
            } else if (JSONHelper.getResultCode(response) == 1010) {
                Toast.makeText(ZZRegisterActivity.this,
                        StringUtils.getString(R.string.phoneisregster), Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        dismissDialog();
        ResultCode.toastVolleyError(ZZRegisterActivity.this, volleyError);
    }
}
